package com.museum.controller;

import com.museum.common.Result;
import com.museum.entity.Visitor;
import com.museum.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/visitor")
public class VisitorController {

    @Autowired
    private VisitorService visitorService;

    @GetMapping("/list")
    public Result<List<Visitor>> list(@RequestParam("userId") Long userId) {
        List<Visitor> list = visitorService.getUserVisitors(userId);
        return Result.success(list);
    }

    @GetMapping("/detail/{id}")
    public Result<Visitor> detail(@PathVariable Long id) {
        Visitor visitor = visitorService.getById(id);
        if (visitor == null) {
            return Result.error("观众信息不存在");
        }
        return Result.success(visitor);
    }

    @PostMapping("/add")
    public Result<Visitor> add(@RequestBody Visitor visitor) {
        visitorService.save(visitor);
        return Result.success(visitor);
    }

    @PutMapping("/update")
    public Result<Visitor> update(@RequestBody Visitor visitor) {
        if (visitor.getId() == null) {
            return Result.error("观众ID不能为空");
        }
        visitorService.updateById(visitor);
        return Result.success(visitor);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = visitorService.removeById(id);
        if (!success) {
            return Result.error("删除失败");
        }
        return Result.success(true);
    }
} 